﻿//---------------------------------------------------------------------------------------------
//  Copyright 2009 Jeremy Palenchar
//  http://identitynotes.palenchar.net
//
//  This file is part of the Identity Notes Sample Custom Activity Library.
//
//  The Identity Notes Sample Custom Activity Library is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  The Identity Notes Sample Custom Activity Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with The Identity Notes Sample Custom Activity Library.  If not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------



using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Text.RegularExpressions;

namespace IdentityNotes.Examples.CustomActivityLibrary
{
    public partial class CalcCellEmailAddressActivity : Activity
	{
        public static DependencyProperty OperatorEmailDictionaryProperty = DependencyProperty.Register(
            "OperatorEmailDictionary", 
            typeof(string[]), 
            typeof(CalcCellEmailAddressActivity));

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Properties")]
        public string[] OperatorEmailDictionary
        {
            get { return (string[])this.GetValue(CalcCellEmailAddressActivity.OperatorEmailDictionaryProperty); }
            set { this.SetValue(CalcCellEmailAddressActivity.OperatorEmailDictionaryProperty, value); }
        }

        public static DependencyProperty CellPhoneEmailAddressProperty = DependencyProperty.Register("CellPhoneEmailAddress", typeof(string), typeof(CalcCellEmailAddressActivity));

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Properties")]
        public string CellPhoneEmailAddress
        {
            get { return (string)this.GetValue(CalcCellEmailAddressActivity.CellPhoneEmailAddressProperty); }
            set { this.SetValue(CalcCellEmailAddressActivity.CellPhoneEmailAddressProperty, value); }
        }

        public static DependencyProperty CellPhoneNumberProperty = DependencyProperty.Register("CellPhoneNumber", typeof(string), typeof(CalcCellEmailAddressActivity));
        
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Properties")]
        public string CellPhoneNumber
        {
            get { return (string)this.GetValue(CalcCellEmailAddressActivity.CellPhoneNumberProperty); }
            set { this.SetValue(CalcCellEmailAddressActivity.CellPhoneNumberProperty, value); }
        }

        public static DependencyProperty CellPhoneOperatorProperty = DependencyProperty.Register("CellPhoneOperator", typeof(string), typeof(CalcCellEmailAddressActivity));

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Properties")]
        public string CellPhoneOperator
        {
            get { return (string)this.GetValue(CalcCellEmailAddressActivity.CellPhoneOperatorProperty); }
            set { this.SetValue(CalcCellEmailAddressActivity.CellPhoneOperatorProperty, value); }
        }


        public CalcCellEmailAddressActivity()
		{
			InitializeComponent();
        }

        protected override ActivityExecutionStatus Execute(
            ActivityExecutionContext executionContext)
        {
            foreach (string operatorEntry in OperatorEmailDictionary)
            {
                string operatorName = operatorEntry.Split(':')[0];
                string operatorAddress = operatorEntry.Split(':')[1];

                if (this.CellPhoneOperator.ToUpperInvariant() == operatorName.ToUpperInvariant())
                {
                    string phoneNumber = Regex.Replace(this.CellPhoneNumber, "\\D", string.Empty); //remove all non-numberical characters
                    phoneNumber = Regex.Replace(phoneNumber, "^1", string.Empty); //remove leading 1 if present
                    this.CellPhoneEmailAddress = phoneNumber + operatorAddress;
                    return base.Execute(executionContext);
                }
            }
            throw new Exception("Operator could not be found - cannot send email");
        }

        
	}
}
